package cn.cvs.inteceptor;

import cn.cvs.util.Constants;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: zwt
 * @Date: 2021/12/14 - 12 - 14 - 8:39
 * @Description: cn.cvs.inteceptor
 * @version: 1.0
 */
//登录拦截器
public class LoginInteceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUser = request.getSession().getAttribute(Constants.USER_SESSION);
        if(loginUser!=null){
            return true;//将请求放行给控制器
        }
        //重定向为登录页面
        //获取动态工程名
        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath+"/toLogin");
        return false;
    }

}
